/********************************************************
 Name          : network.c
 Author        : Khachatur Gyozalyan
 Copyright     : Copyright by Antel Systems
 Description   :
 **********************************************************/
// Include Files
#include "network.h"
#include "tc_1.h"
#include "llc/llc.h"
#include "board.h"
#include "sleep_s.h"
#include "usb_if.h"


xTaskHandle network_task_handle;

unsigned char network_buffer[NETWORK_BUFFER_SIZE_QAM];


//struct udp_pcb *ptel_pcb;
unsigned short port;
struct ip_addr my_ip_addr;
struct ip_addr ip_dest;

unsigned char buuf[] = {"00010203040506070809101112131415161718192021222324252627282930313233343536373839"};



void network_receive(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{ //discomment all this by one step //aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
//	if (p != NULL)
//	{
//		if(p->tot_len > 1024)
//		{
//			return;
//		}
//
//		if(operation_mode.usart_port_mode == DATA_MODE)
//		{
//			usart_put_data((unsigned char *)p->payload,p->tot_len);
//		}
//		else if(operation_mode.usb_port_mode == DATA_MODE)
//		{
//			usb_put_data((unsigned char *)p->payload,p->tot_len);
//		}
//	}
}

//unsigned int Bytes_trensfered = 0;
void network_send(unsigned char *data,unsigned int size, u16_t port)
{ //discomment all this by one step //aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
//	data_type *ptr_data = create_data_type(size);
//	memcpy(ptr_data->data, data, size);
//	//you must set the ptr_data->LinkID, to identifier, the device, which must get this information
////	struct pbuf *p;
////	p = pbuf_alloc(PBUF_TRANSPORT,size,PBUF_RAM);
////	memcpy (p->payload, data,size);
//	//udp_sendto(ptel_pcb, p, &ip_dest, port);
////	Bytes_trensfered += p->tot_len;
////	printk("%d\r\n",Bytes_trensfered);
////	llc_output_data(NULL,p, NULL, port);iiiiiiiiiiiiiiiiiiiiiiiii
//	llc_output_data(ptr_data, 0, PID_UI_FRAME | APP_NETWORK);
//	data_type_free(ptr_data); //De-allocate packet buffer
}

void network_init(void)
{
/*	port = UDP_PORT;
	my_ip_addr.addr = ConfigRoot.WirelessLink.WiFiSettings.IP.value;
	ip_dest.addr = IP_ADDR_BROADCAST->addr;
	ptel_pcb = udp_new();
	if(ptel_pcb)
	{
		udp_bind(ptel_pcb, &my_ip_addr, port);
		udp_recv(ptel_pcb, network_receive, NULL);
	}
	else
	{

	}*/

}


/*
void network_task(void *pvParameters)
{
	unsigned int old_count = 0 ,count = 0;
	int esc_i = 0,firs_Mode_Ready = 0;
	unsigned char Mode_Ready = 0;
	Timer_Tik Waiting_tik = TC1_Get_us();
//	unsigned int network_tik = TC1_Get_us();
	if(StartupMode == START_UP_NORMAL || StartupMode == START_UP_LOAD_DEFAULT)
		vTaskSuspend(cli_task_handle);
	else
		vTaskSuspend(network_task_handle);
	//execute_line("EXIT");
//	exit_to_data_mode();
	printk("Data Mode\r\n");
	portTickType xLastWakeTime;
	xLastWakeTime = xTaskGetTickCount();

	while(1)
	{
		//vTaskDelayUntil(&xLastWakeTime, configTSK_NETWORK_PERIOD);
		if(operation_mode.usart_port_mode == DATA_MODE)
		{
			count += usart_get_data(network_buffer+count,Network_Buffer_Size - count);
		}
		else if(operation_mode.usb_port_mode == DATA_MODE)
		{
			count = usb_get_data(network_buffer,Network_Buffer_Size);
		}
		else
			continue;
		if(Mode_Ready == 1)
		{
			Mode_Ready = 0;
			usart_get_data(network_buffer,1);
			operation_mode.usart_port_mode = CMD_MODE;
			led_dual_mode_blink_end();
			led_mode_set_color(red);
			printk("\r\nCommand Mode\r\n");
			if(firs_Mode_Ready == 0)
			{
				printk(CLI_PROMPT_NORM);
				firs_Mode_Ready = 1;
			}
			//printk("\r\nAntel# ");
			//if(operation_mode.usart_port_mode == DATA_MODE)
			//{
			//	printk("Exit To USART COMMAND MODE\r\n");
			//	operation_mode.usart_port_mode = CMD_MODE;
			//	operation_mode.usb_port_mode = NONE_MODE;
			//	led_mode_set_color(red);
			//	printk("\r\nCommand Mode");
			//	printk("\r\nAntel# ");
			//}
			//else if(operation_mode.usb_port_mode == DATA_MODE)
			//{
			//	printk("Exit To USB COMMAND MODE\r\n");
			//	operation_mode.usart_port_mode = NONE_MODE;
			//	operation_mode.usb_port_mode = CMD_MODE;
			//	led_mode_set_color(red);
			//	printk("\r\nCommand Mode");
			//	printk("\r\nAntel# ");
			//}
			vTaskResume(cli_task_handle);
			vTaskSuspend(network_task_handle);
			continue;
		}

		//count = wifi_get_data(network_buffer,NETWORK_BUFFER_SIZE);

		if(old_count == count-1)
		{
			if(network_buffer[0] == '+')
				esc_i++;
			else
				esc_i = 0;
			if(esc_i == 4)
			{
				Mode_Ready = 1;
				esc_i = 0;
				count = 0;
				continue;
			}
		}
		if(old_count != count)
		{
			if(old_count < count)
				Waiting_tik = TC1_Get_us();
			old_count = count ;
		}
		if((count && ((TC1_Get_us()-Waiting_tik) >= (int)(ConfigRoot.G_Network.SG_NetProtSet.param_protocol_timeout)*10000)) || count >= Network_Buffer_Size/10)
		{
			network_send(network_buffer,count);
			Waiting_tik = TC1_Get_us();
			//network_wifi_send(network_buffer,count);
			count = 0;
			old_count = 0;
		}
		//---------------------------------------------------
		if(operation_mode.usart_port_mode == DATA_MODE)
		{
			usart_output();
		}
	}
}
*/

void network_task(void *pvParameters)
{
	unsigned int count = 0;
	int esc_i = 0,firs_Mode_Ready = 0;
	unsigned char Mode_Ready = 0;
//	unsigned int network_tik = TC1_Get_us();
/*	if(StartupMode == START_UP_NORMAL || StartupMode == START_UP_LOAD_DEFAULT)
		vTaskSuspend(cli_task_handle);
	else
		vTaskSuspend(network_task_handle);*/
	//execute_line("EXIT");
//	exit_to_data_mode();
	printk("Data Mode\r\n");
	portTickType xLastWakeTime;
	xLastWakeTime = xTaskGetTickCount();

	while(1)
	{
		//vTaskDelayUntil(&xLastWakeTime, configTSK_NETWORK_PERIOD);
		if(operation_mode.usart_port_mode == DATA_MODE)
		{
			count = usart_get_data(network_buffer,Network_Buffer_Size);
		}
		else if(operation_mode.usb_port_mode == DATA_MODE)
		{
			count = usb_get_data(network_buffer,Network_Buffer_Size);
		}
		else
			continue;

		sleep_task();

		if(Mode_Ready == 1)
		{
			Mode_Ready = 0;
			usart_get_data(network_buffer,1);
			led_dual_mode_blink_end();
			led_mode_set_color(red);
//			mac_state = MAC_IDLE;
			CMX7164_Reinitialize();
			if(firs_Mode_Ready == 0)
			{
				printk(CLI_PROMPT_NORM);
				firs_Mode_Ready = 1;
			}

			if(active_interface() == USART_IF_ACTIVE)
			{
				operation_mode.usb_port_mode = CMD_MODE;
				printk("\r\nData Mode\r\n");
				operation_mode.usb_port_mode = DATA_MODE;
				operation_mode.usart_port_mode = CMD_MODE;
				printk("\r\nCommand Mode\r\n");
			}
			else if(active_interface() == USB_IF_ACTIVE)
			{
				operation_mode.usart_port_mode = CMD_MODE;
				printk("\r\nData Mode\r\n");
				operation_mode.usart_port_mode = DATA_MODE;
				operation_mode.usb_port_mode = CMD_MODE;
				printk("\r\nCommand Mode\r\n");
			}
			printk(CLI_PROMPT_NORM);

//			vTaskResume(cli_task_handle);
//			vTaskSuspend(network_task_handle);
			continue;
		}

		if(count == 1)
		{
			if(network_buffer[0] == '+')
				esc_i++;
			else
				esc_i = 0;
			if(esc_i == 4)
			{
				Mode_Ready = 1;
				esc_i = 0;
				count = 0;
				continue;
			}
		}
		if(count)
		{
			sleep_mode_init();
			network_send(network_buffer,count,NETWORK_STANDART_PORT);
		}
		//---------------------------------------------------
		if(operation_mode.usart_port_mode == DATA_MODE)
		{
			usart_output();
		}
	}
}


void network_task_init(void)
{
	network_init();
	xTaskCreate(network_task, configTSK_NETWORK_NAME, configTSK_NETWORK_STACK_SIZE, NULL, configTSK_NETWORK_PRIORITY, &network_task_handle);
}

void print_network_stat(void)
{

}


